//
//  KeChengXiangQingCell.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "KeChengXiangQingCell.h"
#import "CourseInfo.h"

@interface KeChengXiangQingCell ()
<
UIWebViewDelegate
>
@property (weak, nonatomic) IBOutlet UILabel *XiangQingTitle;
@property (weak, nonatomic) IBOutlet UILabel *readNum;
@property (weak, nonatomic) IBOutlet CustomLabel *courseType;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
//@property (weak, nonatomic) IBOutlet UIImageView *headImage;

//@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailTwoLable;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webviewHight;

@end

@implementation KeChengXiangQingCell

#define webHight @"webviewHight"


- (void)setModel:(CourseInfo *)model{
    _model = model;
    self.XiangQingTitle.text = model.name;
    self.readNum.text = [NSString stringWithFormat:@"学习人数:%@",model.view_num];
    if ([model.price isEqualToString:@"0"]) {
        self.courseType.text = @"免费";
        self.courseType.layer.borderColor = [[UIColor clearColor] CGColor];
        self.priceLabel.hidden = YES;
        self.courseType.textColor = [UIColor greenColor];
    }else{
       self.priceLabel.text = [NSString stringWithFormat:@"%@学豆",model.price];
        self.courseType.layer.borderColor = [[UIColor redColor] CGColor];
        self.courseType.layer.borderWidth = 1;
    }
    
    self.headImage.layer.masksToBounds = YES;
    self.headImage.layer.cornerRadius = self.headImage.frame.size.width/2;
    
    self.teacherName.text = model.name;
    self.detailTwoLable.text = model.detail;
    
    [self.webView loadHTMLString:model.detail baseURL:nil];
    
    // 赋值部分 NSUserDefaults 高度保存
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    if ([user objectForKey:webHight]) {
        self.webviewHight.constant = [NSString stringWithFormat:@"%@", [user objectForKey:webHight]].floatValue;

        self.webView.delegate = nil;
        [user removeObjectForKey:webHight];
        [user synchronize];

    }else{
        self.webView.delegate  = self;
    }
    
    UIScrollView *first_tempView  = (UIScrollView *)[self.webView.subviews objectAtIndex:0];
    
    first_tempView.scrollEnabled = NO;

    
    
}

#pragma mark ----------------------- webView 代理 加载完后获取高度 ------------------------
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 获取webView的高度
    CGFloat webViewHeight = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    [user setObject:[NSString stringWithFormat:@"%f",webViewHeight] forKey:webHight];

    [user synchronize];
    
    // 发通知告诉tableview 刷新 tableView接收通知然后reloaddata就可以了
    [self sendMessage];
 
}

#pragma mark ----------------------- 获取高度去刷新 ------------------------
-(void)sendMessage
{
    //创建通知
    NSNotification *notification =[NSNotification notificationWithName:rechNotifyName object:nil userInfo:nil];
    //通过通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.courseType.edgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    self.courseType.font = [UIFont systemFontOfSize:14];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
